<?php
/**
 * @version     1.0.0
 * @package     com_brinkmanvm
 * @copyright   Copyright (C) 2013. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Miguel Puig <miguel@freebandtech.com> - http://freebandtech.com
 */
/*
error_reporting(E_ALL);
ini_set('display_errors','On');
*/
// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.controller');

class BrinkmanvmController extends JController
{


    function _deleteTemp()
    {
        $db = JFactory::getDbo();
        $query = 'TRUNCATE TABLE  `bkvm_virtuemart_product_customfields`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_product_customfields` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = 'TRUNCATE TABLE  `bkvm_virtuemart_products`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_products` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = 'TRUNCATE TABLE  `bkvm_virtuemart_products_lang`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_products_lang` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = 'TRUNCATE TABLE  `bkvm_virtuemart_product_categories`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_product_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = "DELETE FROM `bkvm_virtuemart_categories`";
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = "DELETE FROM `bkvm_virtuemart_categories_lang`";
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_categories_lang` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = "DELETE FROM `bkvm_virtuemart_category_categories`";
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_category_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = "DELETE FROM `bkvm_virtuemart_category_medias`";
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_category_medias` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();

        $query = "DELETE FROM `bkvm_virtuemart_product_categories`";
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `bkvm_virtuemart_product_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();



    }

    public function importImages()
    {


        //$zipPath = JPATH_BASE.'/tmp/catalog.zip';
        $zipPath = '/home/upload/catalog.zip';
        $catalogImagesPath = '/var/www/brinkman/brinkmanfiles/images_catalog/product/';

        $zip = new ZipArchive;


        if ($zip->open($zipPath) === TRUE) {
            $zip->extractTo($catalogImagesPath);
            $zip->close();
            echo 'Images added';
        } else {
            echo 'Failed to upload images';
        }



    }
//useful methods
    public function publishAllArticles()
    {
        $db = JFactory::getDbo();
        $query = 'UPDATE `bkvm_virtuemart_products` SET `published` = 1';
        $db->setQuery($query);
        $db->query();

    }
    public function setCategoryLimit()
    {
        $db = JFactory::getDbo();
        $query = 'UPDATE `bkvm_virtuemart_categories` SET `limit_list_initial` = 100';
        $db->setQuery($query);
        $db->query();

    }


    public function show_polish ($text) {

        $text = $this->convert_to($text,'utf-8');
        return $text;
    }
    function convert_to ( $source, $target_encoding )
    {
        // detect the character encoding of the incoming file
        $encoding = mb_detect_encoding( $source, "auto" );

        // escape all of the question marks so we can remove artifacts from
        // the unicode conversion process
        $target = str_replace( "?", "[question_mark]", $source );

        // convert the string to the target encoding
        $target = mb_convert_encoding( $target, $target_encoding, $encoding);

        // remove any question marks that have been introduced because of illegal characters
        $target = str_replace( "?", "", $target );

        // replace the token string "[question_mark]" with the symbol "?"
        $target = str_replace( "[question_mark]", "?", $target );

        return $target;
    }

    function _replaceContent() //Replace temp content in the vm tables
    {
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $langtableCats = $params->get('langtable','#__virtuemart_categories_en_gb');
        $langtableProds = $params->get('langtableprods','#__virtuemart_products_en_gb');

        $db = JFactory::getDbo();
        $query = 'TRUNCATE TABLE  `#__virtuemart_product_customfields`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_product_customfields` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_product_customfields` SELECT * FROM bkvm_virtuemart_product_customfields';
        $db->setQuery($query);
        $db->query();



        $query = 'TRUNCATE TABLE  `#__virtuemart_products`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_products` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_products` SELECT * FROM bkvm_virtuemart_products';
        $db->setQuery($query);
        $db->query();


        $query = 'TRUNCATE TABLE '.$langtableProds.' ';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE ".$langtableProds." AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT '.$langtableProds.' SELECT * FROM bkvm_virtuemart_products_lang';
        $db->setQuery($query);
        $db->query();



        $query = 'TRUNCATE TABLE  `#__virtuemart_product_categories`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_product_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_product_categories` SELECT * FROM bkvm_virtuemart_product_categories';
        $db->setQuery($query);
        $db->query();



        $query = 'TRUNCATE TABLE  `#__virtuemart_categories`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_categories` SELECT * FROM bkvm_virtuemart_categories';
        $db->setQuery($query);
        $db->query();


        $query = 'TRUNCATE TABLE '.$langtableCats;
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE ".$langtableCats." AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT '.$langtableCats.' SELECT * FROM bkvm_virtuemart_categories_lang';
        $db->setQuery($query);
        $db->query();


        $query = 'TRUNCATE TABLE  `#__virtuemart_category_categories`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_category_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_category_categories` SELECT * FROM bkvm_virtuemart_category_categories';
        $db->setQuery($query);
        $db->query();



        $query = 'TRUNCATE TABLE  `#__virtuemart_category_medias`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_category_medias` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_category_medias` SELECT * FROM bkvm_virtuemart_category_medias';
        $db->setQuery($query);
        $db->query();


        $query = 'TRUNCATE TABLE  `#__virtuemart_product_categories`';
        $db->setQuery($query);
        $db->query();
        $query = "ALTER TABLE `#__virtuemart_product_categories` AUTO_INCREMENT=0;";
        $db->setQuery($query);
        $db->query();
        $query = 'INSERT `#__virtuemart_product_categories` SELECT * FROM bkvm_virtuemart_product_categories';
        $db->setQuery($query);
        $db->query();

    }
    function _updateLogs()
    {
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $date =& JFactory::getDate();


        $db = JFactory::getDbo();
        $data =new stdClass();
        $data->date = $date->toFormat();
        $data->products_path = $pathProducts;
        $data->categories_path = $pathCat;
        $db->insertObject( 'bkvm_logs', $data);
        $db->setQuery($query);
        $db->query();

    }

    public function getProducts()
    {
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $products = array();

        $fieldseparator = ";";
        $lineseparator = "\n";
        $file_products_csv = $pathProducts;
        $db = JFactory::getDbo();
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $tableVMLang = 'bkvm_virtuemart_products_lang';
        $allProducts = array();
        $countInsert  = 0;
        $countUpdated = 0;
        $importRelated = $params->get('importrelated',1);

        //Products
        if(!file_exists($file_products_csv)) {
            echo "File ".$file_products_csv." <br />not found. Make sure you specified the correct path.\n";
            exit;
        }
        $file = fopen($file_products_csv,"r");
        if(!$file) {
            echo "Error opening data file.\n";
            exit;
        }
        $size = filesize($file_products_csv);
        if(!$size) {
            echo "File is empty.\n";
            exit;
        }
        $csvcontent = fread($file,$size);
        fclose($file);


        $lines = 0;
        $linearray = array();

        foreach(explode($lineseparator,$csvcontent) as $line):
            $lines++;
            $line = trim($line," \t");
            $line = str_replace("\r","",$line);

            /************************************
            This line escapes the special character. remove it if entries are already escaped in the csv file
             ************************************/
            $line = str_replace("'","\'",$line);
            /*************************************/

            $linearray = explode($fieldseparator,$line);

            $i=0;

            if($linearray[0]):
                $linearray2[0] = $linearray[0];
                $linearray2[1] = $linearray[1];
                $linearray2[2] = $linearray[2];
                $linearray2[3] = $linearray[3];
                $linearray2[4] = $linearray[4];
                $linearray2[5] = $linearray[5];
                $linearray2[6] = $linearray[6];
            endif;


            foreach ($linearray2 as $array2):
                $array2 = str_replace("\"", "", $linearray2);
            endforeach;

            $totmain = count($array2);;
            $tot = count($linearray);

            $rest = (count($linearray))-(count($array2));


            //METO LOS RELACIONADOS separados por comas

            for($i = 22; $i < $tot; $i++){

                if($linearray[$i]!=""):
                    @$related_arr[] = $linearray[$i];
                    $related_arr = str_replace("\"", "", $related_arr);
                endif;

            }

            if(count(@$related_arr)):
                $array2[7] = implode(",",$related_arr);
            else:
                $array2[7] = "NOREL";
            endif;

            unset($related_arr);



            $product =new stdClass();
            $product->product_sku = $this->show_polish($array2[0]);
            $product->item_name = $this->show_polish($array2[1]);
            $product->price = $this->show_polish($array2[2]);
            $product->unit = $this->show_polish($array2[3]);
            $product->unit_convert = $this->show_polish($array2[4]);
            $product->description = $this->show_polish($array2[5]);
            $product->search_info = $this->show_polish($array2[6]);
            $product->related = $this->show_polish($array2[7]);

            $products[] = $product;
        endforeach;

        return $products;
    }

    public function getCategories()
    {

        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $categories = array();


        $fieldseparator = ";";
        $lineseparator = "\n";
        $file_products_csv = $pathCat;
        $tableVMLang = 'bkvm_virtuemart_categories_lang';
        $db = JFactory::getDbo();
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $countInsert  = 0;
        $countUpdated = 0;


        //Groups
        if(!file_exists($file_products_csv)) {
            echo "File ".$file_products_csv." <br />not found. Make sure you specified the correct path.\n";
            exit;
        }
        $file = fopen($file_products_csv,"r");
        if(!$file) {
            echo "Error opening data file.\n";
            exit;
        }
        $size = filesize($file_products_csv);
        if(!$size) {
            echo "File is empty.\n";
            exit;
        }
        $csvcontent = fread($file,$size);
        fclose($file);




        $lines = 0;
        $linearray = array();
        $catsFinal = array();
        $i = 0;

        foreach(explode($lineseparator,$csvcontent) as $line):
            $lines++;
            $line = trim($line," \t");
            $line = str_replace("\r","",$line);
            $line = rtrim($line, ';');

            /************************************
            This line escapes the special character. remove it if entries are already escaped in the csv file
             ************************************/
            $line = str_replace("'","\'",$line);
            /*************************************/

            $linearray = explode($fieldseparator,$line);

            $cat_final =new stdClass();
            $cat_final->alias = $this->show_polish($linearray[0]);
            $cat_final->name = $this->show_polish($linearray[1]);
            $cat_final->description = $this->show_polish($linearray[4]);

            if($linearray[2] == "articles"):
                $cat_final->type = 'articles';

                for ($i = 6; $i <= count($linearray)-1; $i++) {

                    $array_art[$i] = $linearray[$i];

                }
                if(count($array_art)>0):
                    $articulos = implode(",",$array_art);
                else:
                    $articulos = "0";
                endif;
                unset($array_art);
                $cat_final->sub = $articulos;

            endif;

            if($linearray[2] == "subgroup"):
                $cat_final->type = 'subgroup';
                for ($i = 6; $i <= count($linearray)-1; $i++) {

                    $array_art[$i] = $linearray[$i];

                }
                if(count($array_art)>0):
                    $subgroups = implode(",",$array_art);
                else:
                    $subgroups = "No Subgroups";
                endif;

                unset($array_art);
                $cat_final->sub = $subgroups;


            endif;


            // 1.Add/update all the categories at once

            $category =new stdClass();
            $category->virtuemart_vendor_id = 1;
            $category->limit_list_initial = 100;
            $category->shared = 1; //flag
            $category->published = 1;
            $category->created_on = $date->toFormat();
            $category->modified_on = $date->toFormat();
            $category->modified_by = $superAdminId;



            $category->metadesc = $this->show_polish($linearray[0]);
            $category->category_name = $this->show_polish($linearray[1]);
            if($linearray[4]!=''): $category->category_description = $this->show_polish($linearray[4]); endif;
            $category->slug =  JFilterOutput::stringURLSafe($this->show_polish($linearray[0]));
            $category->metakey =  $this->show_polish($linearray[5]);

            $category->type = $cat_final->type;
            $category->sub = $cat_final->sub;

            $categories[] = $category;
            $i++;


        endforeach;


        return $categories;
    }

    public function getFeatured()
    {
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $langtableProds = $params->get('langtableprods','#__virtuemart_products_en_gb');

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('product_sku');
        $query->from('#__virtuemart_products');
        $query->where('product_special = 1');
        $db->setQuery($query);
        $featured = $db->loadObjectList();
        return $featured;

    }
    function _insertProducts($products)
    {
        $db = JFactory::getDbo();
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $tableVMLang = 'bkvm_virtuemart_products_lang';
        $importRelated = $params->get('importrelated',1);



        //Insert products in temp tables
        foreach ($products as $product):
            $data =new stdClass();
            $data->product_sku = $product->product_sku;
            $data->created_on = $date->toFormat();
            $data->modified_on = $date->toFormat();
            $data->created_by = $superAdminId;
            $data->product_unit = 1;
            $data->published = 1;
            try {
                $db->insertObject( 'bkvm_virtuemart_products', $data);
                $productId = $db->insertid();

            } catch (Exception $e) {
                echo $e;
            }


            //j25_virtuemart_products_lang
            $data =new stdClass();
            $data->virtuemart_product_id = $productId;
            $data->product_name = $product->item_name;
            $data->product_desc = $product->description;
            $data->product_s_desc = $product->unit_convert;
            $data->metakey = $product->unit;
            $data->metadesc = $product->search_info;
            $data->slug = JFilterOutput::stringURLSafe($product->item_name);
            try {
                $db->insertObject( $tableVMLang, $data);

            } catch (Exception $e) {
                echo $e;
            }



        endforeach;

    }
    function _insertRelatedProducts($products)
    {
        $db = JFactory::getDbo();
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $tableVMLang = 'bkvm_virtuemart_products_lang';
        $importRelated = $params->get('importrelated',1);

        $db = JFactory::getDbo();
        $date = JFactory::getDate();

        foreach($products as $product):
            if($product->related != 'NOREL'):

                $query = 'SELECT virtuemart_product_id FROM `bkvm_virtuemart_products` WHERE product_sku = '.$product->product_sku;
                $db->setQuery($query);
                $productId = $db->loadResult();

                $prodRels = explode(",", $product->related);
                foreach ($prodRels as $pr):
                    $query = 'SELECT virtuemart_product_id FROM `bkvm_virtuemart_products` WHERE product_sku = '.$pr;
                    $db->setQuery($query);
                    $virtuemart_product_id = $db->loadResult();
                    if($virtuemart_product_id):
                        $data =new stdClass();
                        $data->virtuemart_product_id = $productId;
                        $data->virtuemart_custom_id = 1;
                        $data->custom_value = $virtuemart_product_id;
                        $data->modified_by = $superAdminId;
                        $data->modified_on = $date->toFormat();
                        $db->insertObject( 'bkvm_virtuemart_product_customfields', $data);
                    endif;
                endforeach;
            endif;
        endforeach;

    }
    function _insertCategories($categories)
    {
        $db = JFactory::getDbo();
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $tableVMLang = 'bkvm_virtuemart_categories_lang';
        $importRelated = $params->get('importrelated',1);

        $db = JFactory::getDbo();
        $date = JFactory::getDate();

        foreach ($categories as $key=>$categorySingle):
            if($key!=0): //prodcutKatalogus (header CSV)
                $category =new stdClass();
                $category->virtuemart_vendor_id = 1;
                $category->limit_list_initial = 100;
                $category->shared = 1; //flag
                $category->published = 1;
                $category->created_on = $date->toFormat();
                $category->modified_on = $date->toFormat();
                $category->modified_by = $superAdminId;
                try {
                    $db->insertObject( 'bkvm_virtuemart_categories', $category);
                    $categoryId = $db->insertid();
                    $categories[$key]->virtuemart_category_id = $categoryId;
                } catch (Exception $e) {
                    echo $e;
                }


                $categoryLang =new stdClass();
                $categoryLang->metadesc = $categorySingle->metadesc;
                $categoryLang->category_name = $categorySingle->category_name;
                $categoryLang->category_description = $categorySingle->category_description;
                $categoryLang->slug =  $categorySingle->slug;
                $categoryLang->metakey =  $categorySingle->metakey;
                $categoryLang->virtuemart_category_id = $categoryId;
                try {
                    $db->insertObject( $tableVMLang, $categoryLang);
                } catch (Exception $e) {
                    echo $e;
                }
                /*

                            $data =new stdClass();
                            $data->category_parent_id = 0;
                            $data->category_child_id = $categoryId;
                            try {
                                $db->insertObject( 'bkvm_virtuemart_category_categories', $data);
                            } catch (Exception $e) {
                                echo $e;
                            }
                */
            endif;
        endforeach;


        return $categories;
    }
    function _insertSubCategories($categories)
    {
        $db = JFactory::getDbo();
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $tableVMLang = 'bkvm_virtuemart_categories_lang';
        $importRelated = $params->get('importrelated',1);

        $db = JFactory::getDbo();
        $date = JFactory::getDate();

        foreach ($categories as $key=>$cat):

            if($cat->type=='subgroup'):
                $subgroups = explode(',',$cat->sub);

                foreach($subgroups as $sg):
                    $query = $db->getQuery(true);
                    $query->select('virtuemart_category_id');
                    $query->from($tableVMLang);
                    $query->where('metadesc = "'.$sg.'"');
                    $db->setQuery($query);
                    $sgId = $db->loadResult();

                    if($sgId):
                        $data =new stdClass();
                        $data->category_parent_id = $cat->virtuemart_category_id;
                        $data->category_child_id = $sgId;
                        $db->insertObject( 'bkvm_virtuemart_category_categories', $data);
                    endif;
                endforeach;
            endif;

            if($cat->type=='articles'):
                $subarticles = explode(',',$cat->sub);
                foreach($subarticles as $sa):
                    $query = 'SELECT virtuemart_product_id FROM `bkvm_virtuemart_products` WHERE product_sku = "'.$sa.'"';
                    $db->setQuery($query);
                    $virtuemart_product_id = $db->loadResult();
                    if($virtuemart_product_id):
                        $data =new stdClass();
                        $data->virtuemart_product_id = $virtuemart_product_id;
                        $data->virtuemart_category_id = $cat->virtuemart_category_id;
                        $db->insertObject( 'bkvm_virtuemart_product_categories', $data);
                    endif;
                endforeach;
            endif;

        endforeach;


    }
    function _updateFeatured($featured)
    {
        $db = JFactory::getDbo();
        $params = JComponentHelper::getParams('com_brinkmanvm');
        $pathCat = $params->get('pathcategories','');
        $pathProducts = $params->get('pathproducts','');
        $superAdminIdJoomla = $params->get('superAdminIdJoomla',64);
        $date = JFactory::getDate();
        $superAdminId = $superAdminIdJoomla;
        $tableVMLang = 'bkvm_virtuemart_categories_lang';
        $importRelated = $params->get('importrelated',1);

        $db = JFactory::getDbo();
        $date = JFactory::getDate();
        foreach ($featured as $product):
            $db = JFactory::getDbo();
            $query = 'UPDATE `bkvm_virtuemart_products` SET `product_special` = 1 WHERE product_sku="'.$product->product_sku.'"';
            $db->setQuery($query);
            $db->query();
        endforeach;
    }
    public function process()
    {
        echo "Import done...";

        $products = $this->getProducts();

        $categories = $this->getCategories();


        $featured = $this->getFeatured();

        //Delete temp tables
        $this->_deleteTemp();
        //Insert Products
        $this->_insertProducts($products);
        //Insert related Products
        $this->_insertRelatedProducts($products);
        //Insert Categories
        $newCategories = $this->_insertCategories($categories);
        //Insert subcategories
        $this->_insertSubCategories($newCategories);
        //Update featured
        $this->_updateFeatured($featured);
        //Import images
        $this->importImages();

        $this->_replaceContent();
        $this->_updateLogs();

    }
    public function logs()
    {
        $this->_updateLogs();
        echo "logs imported";
    }

}